﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Data.Sql;
using System.Text;

public partial class Agency_Default : System.Web.UI.Page
{
  

    protected void Page_Load(object sender, EventArgs e)
    {
        string strSql;
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {

            if (Request.Form["btnQBSQCompany"] == "全部授权")
            {
                Db db = new Db();
                strSql = "update company set CompanyUpdateStatus=1  ";
                db.Command(strSql);
                db.Close();
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "company", strSql, "../Agency/Default.aspx");
            }
            if (Request.Form["btnQBQXCompany"] == "全部取消")
            {
                Db db = new Db();
                strSql = "update company set CompanyUpdateStatus=0";
                db.Command(strSql);
                db.Close();
            }
            if (Request.Form["btnSQCompany"] == "授  权")
            {
                //08.11 增加了try{}catch{}     
                try
                {
                    string delAccountId = Request.Form["chk"].ToString();
                    Db db = new Db();
                    strSql = "update company set CompanyUpdateStatus=1 where  AccountId in (" + delAccountId + ")";
                    db.Command(strSql);
                    db.Close();
                    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "company", strSql, "../Agency/Default.aspx");
                }
                catch
                { }
            }
            if (Request.Form["btnQXCompany"] == "取  消")
            {
                //08.11 增加了try{}catch{}     
                try
                {
                    string delAccountId = Request.Form["chk"].ToString();
                    Db db = new Db();
                    strSql = "update company set CompanyUpdateStatus=0 where  AccountId in (" + delAccountId + ")";
                    db.Command(strSql);
                    db.Close();
                    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "company", strSql, "../Agency/Default.aspx");
                }
                catch
                { }
            }
            if (Request.Form["btnQBSQProduct"] == "全部授权")
            {
                Db db = new Db();
                strSql = "update company set ProductAddStatus=1 where sort=0";
                db.Command(strSql);
                db.Close();
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "company", strSql, "../Agency/Default.aspx");
            }
            if (Request.Form["btnQBQXProduct"] == "全部取消")
            {
                Db db = new Db();
                strSql = "update company set ProductAddStatus=0 ";
                db.Command(strSql);
                db.Close();
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "company", strSql, "../Agency/Default.aspx");
            }
            if (Request.Form["btnSQProduct"] == "授  权")
            {
                //08.11 增加了try{}catch{}     
                try
                {
                    string delAccountId = Request.Form["chk"].ToString();
                    Db db = new Db();
                    strSql = "update company set ProductAddStatus=1 where AccountId in (" + delAccountId + ")";
                    db.Command(strSql);
                    db.Close();
                    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "company", strSql, "../Agency/Default.aspx");
                }
                catch { }
            }
            if (Request.Form["btnQXProduct"] == "取  消")
            {
                //08.11 增加了try{}catch{}     
                try
                {
                    string delAccountId = Request.Form["chk"].ToString();
                    Db db = new Db();
                    strSql = "update company set ProductAddStatus=0 where  AccountId in (" + delAccountId + ")";
                    db.Command(strSql);
                    db.Close();
                    HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "company", strSql, "../Agency/Default.aspx");
                    
                }
                catch { }
            }
            GetYBM();
            showGridView();
        }

    }
    protected void Button5_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {
        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "Company";                                               //要查询的表明
        dbPage.Key = "accountid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "accountid,CompanyName,UserCode,case ProductAddStatus when  1 then '已授权' else '未授权'  end ProductAddStatus, case CompanyUpdateStatus when  1 then '已授权' else '未授权'  end CompanyUpdateStatus ";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 10;                                                    //没有显示记录条数   
        dbPage.OrderBy = "UserCode desc,CompanyName";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = "", CompanyName = "", _UserCode;
        CompanyName = txtAttorneyName.Text.ToString().Trim();
        _UserCode = txtUserCode.Text.ToString().Trim();

        if (_UserCode != "")
        {
            strtemp = strtemp + " UserCode like '%" + _UserCode + "%' and";
        }
        if (CompanyName != "")
        {
            strtemp = strtemp + " CompanyName like '%" + CompanyName + "%' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
        return strtemp;
    }
    public void Print()
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("审核通过的企业信息.xls"));
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.dv.RenderControl(oHtmlTextWriter);

        // 把文件流发送到客户端 
        Response.Write(oStringWriter.ToString());
        // 停止页面的执行

        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }


    #region IPageDescribe 成员

    public string Name
    {
        get { return "企业维护信息开关"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "企业维护信息开关"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);

            return _AccountType;
        }
    }

    #endregion
    protected void Button1_Click(object sender, EventArgs e)
    {
        string StrSqlsele = "select usercode as 企业注册号,CompanyName as 企业名称,Address as 地址,Telepone as 电话,Fax as 传真,Lawer as 法人 ,Email as 电子邮箱, PostCode as 邮政编号,NetAddress as 网址,case CheckStatus when '1' then '审核通过' else '审核不通过' end as 审核状态 from company order by CheckStatus ";

        Db db = new Db();
        System.Data.IDataReader dr;
        dr = db.DataReader(StrSqlsele);
        dv.DataSource = dr;
        dv.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        Print();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string StrSqlsele = " select 地区,注册号,医院名称,sum(挂网数) as 挂网数,sum(提交数) as 提交数,sum(交易中) as 交易中,sum(入库数) as 入库数 from(select *from   [hospitalguawang] union select *from [hositaltijiao] union select *from [hospitaljiaoyi] union select * from  [hospitalruku]) as a group by 地区,注册号,医院名称";

        Db db = new Db();
        System.Data.IDataReader dr;
        dr = db.DataReader(StrSqlsele);
        dv.DataSource = dr;
        dv.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        Print1();
    }
    private void Print1()
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("挂网目录.xls"));
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.dv.RenderControl(oHtmlTextWriter);

        // 把文件流发送到客户端 
        Response.Write(oStringWriter.ToString());
        // 停止页面的执行

        Response.End();
    }
    //控制企业预报名
    protected void Button7_Click(object sender, EventArgs e)
    {
        string strSql = "";
        if (Button7.Text.Equals("关闭企业预报名"))
        { strSql = "update account set isTrade_landing=0"; }
        else
        {
            strSql = "update account set isTrade_landing=1";
        }
        Db db = new Db();
        db.Command(strSql);
        db.Close();
        Response.Write("<script>alert('修改成功！');</script>");
        GetYBM();
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "company", strSql, "../Agency/Default.aspx");
    }

    // 显示预报名状态
    private void GetYBM()
    {
        string _strsele = "select isnull(isTrade_landing,0) as YBMstatus from account where accountid=1";
        Db db = new Db();
        DataTable dt = db.DataTable(_strsele);
        db.Close();
        if (dt != null && Convert.ToInt32(dt.Rows[0][0].ToString()) > 0)
        {
            Button7.Text="关闭企业预报名";
        }
        else
        {
            Button7.Text = "开启企业预报名";
        }
        dt.Dispose();
    }
}
